<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>indexBy</title>
</head>

<body>
    <div id="content"></div>
    <script>
        const indexBy = (arr, handler) => arr.reduce((obj, v, i) => {
            obj[handler(v, i, arr)] = v;
            return obj;
        }, {});
        document.querySelector("#content").textContent = JSON.stringify(indexBy([{
                id: 10,
                name: 'apple'
            },
            {
                id: 20,
                name: 'orange'
            }
        ], x => x.id));
        // { '10': { id: 10, name: 'apple' }, '20': { id: 20, name: 'orange' } }
    </script>
</body>

</html>